<?php

namespace Admin\Controller;
class LogController extends AdminController
{
    public function index()
    {
        $this->display("index");
    }

    public function view()
    {
        $n = M("Log");
        import("ORG.Util.Page");
        //导入分页类
        $page = $_POST["page"];
        $rows = $_POST["rows"];
        $sort = $_POST["sort"];
        $order = $_POST["order"];
        if (I("username") != null && I("username") != "") {
            $username = I("username");
            $condition['username'] = array('like', '%' . $username . '%');
        }
        $starttime = I("starttime");
        $endtime = I("endtime");
        if (!empty($starttime) && !empty($endtime)) {
            $condition['time'] = array('between', array($starttime, $endtime));
        }
        if (I("type") != null && I("type") != "") {
            $type = I("type");
            $condition['type'] = array('like', '%' . $type . '%');
        }
        if (I("ip") != null && I("ip") != "") {
            $ip = I("ip");
            $condition['ip'] = array('like', '%' . $ip . '%');
        }
        if (I("remark") != null && I("remark") != "") {
            $remark = I("remark");
            $condition['remark'] = array('like', '%' . $remark . '%');
        }
        $count = $n->where($condition)->count();
        //计算总数
        $list = $n->where($condition)->page($page, $rows)->order(array($sort => $order))->select();
        $list = json_encode($list);
        if ($count != 0) {
            $result = '{"total":' . $count . ',"rows":' . $list . '}';
        } else {
            $result = '{"total":' . $count . ',"rows":""}';
        }
        echo($result);
    }

    //添加提交处理
    public function add()
    {
        $m = D("Log");
        if (!$m->create()) {
            $this->error($m->getError(), 3);
        } else {
            if ($result = $m->add()) {
                $this->success('添加成功！', 1);
            } else {
                $this->error('添加失败！', 0);
            }
        }
    }

    // 删除数据
    public function delete()
    {
        if (!empty($_GET['ids'])) {
            $d = D("Log");
            $condition['id'] = array('in', $_GET['ids']);
            $result = $d->where($condition)->delete();
            if (false !== $result) {
                $this->success('删除成功！', 1);
            } else {
                $this->error('删除出错！', 1);
            }
        } else {
            $this->error('删除项不存在！', 1);
        }
        //$this->redirect('index');
    }

    // 更新数据
    public function edit()
    {
        $Form = D("Log");
        if ($vo = $Form->create()) {
            $list = $Form->save($_POST);
            if ($list !== false) {
                $this->success('更新成功！', 1);
            } else {
                $this->error('没有更新任何数据!', 0);
            }
        } else {
            $this->error($Form->getError(), 3);
        }
    }

    // 删除数据
    public function deleteByCon()
    {
        $d = D("Log");
        if (I("username") != null && I("username") != "") {
            $username = I("username");
            $condition['username'] = array('like', '%' . $username . '%');
        }
        $starttime = I("starttime");
        $endtime = I("endtime");
        if (!empty($starttime) && !empty($endtime)) {
            $condition['time'] = array('between', array($starttime, $endtime));
        }
        if (I("type") != null && I("type") != "") {
            $type = I("type");
            $condition['type'] = array('like', '%' . $type . '%');
        }
        if (I("ip") != null && I("ip") != "") {
            $ip = I("ip");
            $condition['ip'] = array('like', '%' . $ip . '%');
        }
        if (I("remark") != null && I("remark") != "") {
            $remark = I("remark");
            $condition['remark'] = array('like', '%' . $remark . '%');
        }

        $result = $d->where($condition)->delete();
        if (false !== $result) {
            $this->success('删除成功！', 1);
        } else {
            $this->error('删除出错！', 1);
        }
    }

}

?>